﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc.Html;
using SharpDom;
using SharpDom.Mvc;

namespace Bzure.Apps.Security.Views
{
    public class InternalMaster : SiteMaster
    {
        string Script1()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" var CurrentCheck = function (owner, self) {");
            sb.Append("  if (contains(document.location.href,$(self).attr(\"href\"),true)) {");
            sb.Append(" $(owner).removeClass();");
            sb.Append(" $(owner).addClass(\"nav-top-item current\");");
            sb.Append(" $(self).addClass(\" current\");");
            sb.Append("} };");
            sb.Append("  $(document).ready(function () {");
            sb.Append(" CurrentCheck(\"#mi-user\", \"#mi-userlist\");");
            sb.Append("CurrentCheck(\"#mi-role\", \"#mi-newrole\");");
            sb.Append("CurrentCheck(\"#mi-role\",\"#mi-rolelist\");");
            sb.Append("});");
            return sb.ToString();
        }
        string Script2(string k, KeyValuePair<string, string> pair)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("    $(document).ready(function () {");
            sb.AppendFormat("CurrentCheck(\"#mi-droitconfig-{1}\", \"#mi-droit-{0}\");", pair.Value, k);
            sb.Append("   });");
            return sb.ToString();
        }
        public override ContainerTag CreateTagTree(ContainerTag container)
        {
            return container
            [
                html.attr(xmlns: "http://www.w3.org/1999/xhtml")
                [
                    head
                    [
                        title[PlaceHolder(TitleContent)],
                        meta.attr(httpequiv: HttpEquiv.content_type, content: "text/xml; charset=UTF-8"),
                        link.attr(href: Url.RouteUrl("ResourceAccess_Image", new { location = "SecurityApp", id = "favicon.ico" })).ext("rel", "shortcut icon").ext("type", "image/x-icon"),
                        link.attr(href: Url.RouteUrl("ResourceAccess_Image", new { location = "SecurityApp", id = "favicon.ico" }), rel: LinkType.bookmark),
                        link.attr(href: Url.RouteUrl("ResourceAccess_CSS", new { location = "SecurityApp", id = "reset.css" }), rel: LinkType.stylesheet, type: MimeType.text.css),
                        link.attr(href: Url.RouteUrl("ResourceAccess_CSS", new { location = "SecurityApp", id = "style.css" }), rel: LinkType.stylesheet, type: MimeType.text.css),
                        link.attr(href: Url.RouteUrl("ResourceAccess_CSS", new { location = "SecurityApp", id = "invalid.css" }), rel: LinkType.stylesheet, type: MimeType.text.css),
                        link.attr(href: Url.RouteUrl("ResourceAccess_CSS", new { location = "SecurityApp", id = "public.css" }), rel: LinkType.stylesheet, type: MimeType.text.css),
                        link.attr(href: Url.RouteUrl("ResourceAccess_CSS", new { location = "SecurityApp", id = "checkbox.css" }), rel: LinkType.stylesheet, type: MimeType.text.css),
                        script.attr(type: MimeType.text.javascript, src: Url.RouteUrl("ResourceAccess_Script", new { location = "SecurityApp", id = "jquery-1.7.2.min.js" })),
                        script.attr(type: MimeType.text.javascript, src: Url.RouteUrl("ResourceAccess_Script", new { location = "SecurityApp", id = "Common.js" })),
                        script.attr(type: MimeType.text.javascript, src: Url.RouteUrl("ResourceAccess_Script", new { location = "SecurityApp", id = "jquery.checkbox.js" })),
                       script.attr(type: MimeType.text.javascript)[Script1()]
                   
                     
                    ],
                    body
                    [
                          div.attr(id: "body-wrapper")
                          [
                             div.attr(id: "sidebar")
                             [
                               div.attr(id: "sidebar-wrapper")
                                  [
                                     div.attr(id: "sidebar-title")[p["通用权限管理"]],
                                     div.attr(id: "profile-links")
                                      [
                                          If(Actor.Public.IsAuthenticated, Then: c => c
                                            [
                                              " 您好, " + Actor.Me.Principal.Identity.Name + "&nbsp;&nbsp;|&nbsp;&nbsp;",
                                              a.attr(href: Actor.Public.UserLogOffUrl())["注销"],
                                              br,
                                              br
                                            ],
                                          Else: c => c
                                            [
                                              a.attr(href: Actor.Public.UserLogOnUrl())["登录"],
                                              br,
                                              br
                                            ]
                                        )
                                      ],
                                     ul.attr(id: "main-nav")
                                      [
                                           li[
                                               a.attr(@class: "nav-top-item", id: "mi-user")["用户"],
                                               ul[
                                                   li[
                                                   a.attr(id: "mi-userlist", href: Actor.Public.UserListUrl())["用户列表"]
                                                     ]
                                                   ]
                                             ],
                                           li[
                                               a.attr(@class: "nav-top-item", id: "mi-role")["角色"],
                                               ul[
                                                   li[
                                                   a.attr(id: "mi-newrole", href: Actor.Public.RoleNewUrl())["新建角色"]
                                                     ],
                                                   li[
                                                   a.attr(id: "mi-rolelist", href: Actor.Public.RoleListUrl())["角色列表"]
                                                     ]
                                                   ]
                                             ],
                                             ForEach(Actor.Public.DroitsConfigUrl(), (itm, b) => b[

                                           li[
                                               a.attr(@class: "nav-top-item", id: "mi-droitconfig-" + itm.Key)["授权->" + itm.Key],
                                               ul[
                                                   ForEach(itm.Value, (item, c) => c[
                                                                 li[
                                                                 script.attr(type: MimeType.text.javascript)[Script2(itm.Key ,item)],
                                                                 a.attr(id: "mi-droit-" + item.Value, href: item.Key)[item.Value]
                                                                  ]
                                                           ])
                                                  ]
                                             ]


                                          ])

                                      ]
                                  ]
                             ],
                             div.attr(id: "main-content")
                             [
                              PlaceHolder(MainContent),
                              div.attr(@class: "clear"),
                              div.attr(id: "footer")
                                      [
                                      small
                                             [
                                             " &#169; Copyright 2012",
                                              a.attr(href: "http://www.bzure.cn")["bzure.cn"],
                                              " 版权所有 |",
                                              a.attr(href: "#")["Top"]
                                             ]
                                      ]
                             ]
                         ],
                              script.attr(type: MimeType.text.javascript, src: Url.RouteUrl("ResourceAccess_Script", new { location = "SecurityApp", id = "simpla.jquery.configuration.js" }))


                    ]
                ]
            ];
        }
    }
}
